package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.annotation.RequriedPermission;
import cn.wolfcode.wms.domain.Employee;
import cn.wolfcode.wms.util.AjaxResult;
import cn.wolfcode.wms.query.EmployeeQuery;
import cn.wolfcode.wms.service.IDepartmentService;
import cn.wolfcode.wms.service.IEmployeeService;
import cn.wolfcode.wms.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;
    @RequriedPermission("员工列表")
    @RequestMapping("/query")
    public String query(Model model, @ModelAttribute("qo")EmployeeQuery qo){
        PageInfo<Employee> result = employeeService.query(qo);
        model.addAttribute("result",result);
        model.addAttribute("depts",departmentService.list());
        return "employee/list";
    }
    @RequriedPermission("员工编辑")
    @RequestMapping("input")
    public String input(Model model,Long id){
        if (id != null){
            Employee employee = employeeService.get(id);
            model.addAttribute("employee",employee);
        }
        model.addAttribute("depts",departmentService.list());
        model.addAttribute("roles",roleService.list());
        return "employee/input";
    }
    @RequriedPermission("员工保存/更新")
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public AjaxResult saveOrUpdate(Employee employee,Long[] ids){
        if (employee.getId() == null){
            employeeService.save(employee,ids);
        }else {
            employeeService.update(employee,ids);
        }
        return new AjaxResult();
    }
    @RequriedPermission("员工删除")
    @RequestMapping("delete")
    @ResponseBody
    public AjaxResult delete(Long id){
        if (id != null){
            employeeService.delete(id);
        }
        return new AjaxResult();
    }
}
